package typoker.logic;

import java.util.Random;

import typoker.GameProxy;
import typoker.utils.Util;

public class Chat implements Runnable {
	private static final String[] phrases = Util.readStringFromFile("chat.dat");

	private static Random random = new Random();

	private boolean gameOver;
	private String[] names;

	public Chat(Player[] players, int human) {
		names = new String[players.length - 1];
		for (int i = 0; i < names.length; i++) {
			if (i != human) {
				names[i] = players[i].getName();
			} else {
				names[i] = players[names.length].getName();
			}
		}

		gameOver = false;
		new Thread(this).start();
	}

	public void run() {
		while (!gameOver) {
			GameProxy.addMessage(names[random.nextInt(names.length)], phrases[random.nextInt(phrases.length)]);
			sleep(500 + random.nextInt(9500));
		}
	}
	
	public void stop() {
		gameOver = true;
	}

	private void sleep(int n) {
		try {
			Thread.sleep(n);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}